package com.ruoyi.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Medicine;
import com.ruoyi.system.service.IMedicineService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品列表Controller
 *
 * @author yangxp
 * @date 2020-07-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/medicine/medicine")
public class MedicineController extends BaseController {

	private final IMedicineService iMedicineService;

	/**
	 * 查询药品列表列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicine:list')")
	@GetMapping("/list")
	public TableDataInfo list(Medicine medicine) {
		startPage();
		QueryWrapper<Medicine> lqw = new QueryWrapper<>();

		if (StringUtils.isNotBlank(medicine.getSheCode())) {
			lqw.like("she_code", medicine.getSheCode());
		}

		if (StringUtils.isNotBlank(medicine.getMedName())) {
			lqw.like("med_name", medicine.getMedName());
		}

		List<Medicine> list = iMedicineService.listMedicine(lqw);
		list.forEach(item -> item.setStock(NumberUtil.div(item.getStock(), 1000)));
		return getDataTable(list);
	}

	/**
	 * 导出药品列表列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicine:export')")
	@Log(title = "药品列表", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(Medicine medicine) {
		LambdaQueryWrapper<Medicine> lqw = new LambdaQueryWrapper<Medicine>(medicine);
		List<Medicine> list = iMedicineService.listMedicine(lqw);
		ExcelUtil<Medicine> util = new ExcelUtil<Medicine>(Medicine.class);
		return util.exportExcel(list, "medicine");
	}

	/**
	 * 获取药品列表详细信息
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicine:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(iMedicineService.getById(id));
	}

	/**
	 * 新增药品列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicine:add')")
	@Log(title = "药品列表", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Medicine medicine) {
		medicine.setMedId(IdUtil.fastSimpleUUID());
		medicine.setGmtCreate(DateUtil.date());
		medicine.setGmtModified(DateUtil.date());
		BigDecimal stock = medicine.getStock();
		//千克换算成克
		medicine.setStock(NumberUtil.mul(stock, 1000));
		return toAjax(iMedicineService.save(medicine) ? 1 : 0);
	}

	/**
	 * 修改药品列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicine:edit')")
	@Log(title = "药品列表", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Medicine medicine) {
		return toAjax(iMedicineService.updateById(medicine) ? 1 : 0);
	}

	/**
	 * 删除药品列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicine:remove')")
	@Log(title = "药品列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(iMedicineService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
	}


}
